package DataAnalysis_qcl

import org.apache.spark.sql.SparkSession

import java.util.Properties

object DataAnalysis02 {
  def main(args: Array[String]): Unit = {
    /*
          DataAnalysis02:分析北京的租房信息里面每个区的有电梯和无电梯的租房信息
     */
  //  准备sparksql的环境
    val spark=SparkSession.builder()
    .master("local[*]")
    .appName("DataAnalysis02")
    .enableHiveSupport()
    .getOrCreate()


    //  准备连接mysql的配置
    val mysql_connect=new Properties()
    mysql_connect.setProperty("user","root")
    mysql_connect.setProperty("password","123456")
    mysql_connect.setProperty("driver","com.mysql.jdbc.Driver")

    //  读取mysql处理完的数据
    spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/qcl?useSSL=false","data_processing",mysql_connect)
      .createOrReplaceTempView("data")

    spark.sql("use qcl")

  val result=  spark.sql(
      """
        |select
        |downtown,
        |elevator,
        |count(*) as number
        |from data
        |group by downtown,elevator
        |order by number desc
        |""".stripMargin)


    result.write.mode("overwrite")
      .jdbc("jdbc:mysql://192.168.40.110:3306/qcl?useSSL=false", "data_analysis02", mysql_connect)

    //  将结果存入hdfs
    result.repartition(1).write.mode("overwrite")
      .csv("/qcl/data_analysis/data_analysis02.csv")



    //  关闭环境
    spark.close()

  }

}
